package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.VCarMoneyStatistic;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.ColorUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SelectedValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

/**
 * Created by hacker on 2017/7/26.
 */

public class CarMoneyStatisticAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<VCarMoneyStatistic> carMoneyStatistics;
    private Context mContext;
    private int dataType;
    // 1运输公司充值 2.汽修厂充值 3.加油站充值 4运输公司取现 5.加油站取现
    public static final int TYPE_CZ_YSGS = 1;
    public static final int TYPE_CZ_QXC = 2;
    public static final int TYPE_CZ_JYZ = 3;
    public static final int TYPE_CZ_YWSBFH = 4;

    //1修车 2加油 3取现 4冲油卡  6转账 7业务取消
    public static final int TYPE_OUT_XC = 1;
    public static final int TYPE_OUT_JY = 2;
    public static final int TYPE_OUT_QX = 3;
    public static final int TYPE_OUT_CYC = 4;
    public static final int TYPE_OUT_ZZ = 6;
    public static final int TYPE_OUT_YWQX = 7;

    public CarMoneyStatisticAdapter(List<VCarMoneyStatistic> list, Context context, int type) {
        this.mContext = context;
        this.carMoneyStatistics = list;
        this.dataType = type;
    }

    @Override
    public int getItemViewType(int position) {
        if (!carMoneyStatistics.isEmpty()) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_statistic_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder headerViewHolder = (ViewHolder) holder;
            headerViewHolder.BindViewData(carMoneyStatistics);
            headerViewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public int getItemColumnSpan(int position) {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView nestBtn;
        PieChartView pieChartView;
        TextView inWeight;
        TextView outWeight;
        TextView differWeight;
        private int lastPosition = -1;
        private int basePosition;
        private List<SliceValue> sliceValueList;

        public ViewHolder(View itemView) {
            super(itemView);
            nestBtn = (CircleImageView) itemView.findViewById(R.id.next_btn);
            pieChartView = (PieChartView) itemView.findViewById(R.id.pieChartView);
            inWeight = (TextView) itemView.findViewById(R.id.in_weight);
            outWeight = (TextView) itemView.findViewById(R.id.out_weight);
            differWeight = (TextView) itemView.findViewById(R.id.differ_weight);
        }

        public void BindViewData(final Object object) {
            String subText = "对比图";
            String centerText = "";
            SliceValue sliceValue;
            sliceValueList = new ArrayList<>();
            switch (dataType) {
                case 1:
                    centerText = "入账";
                    break;
                case 2:
                    centerText = "出账";
                    break;
            }
            for (int i = 0; i < carMoneyStatistics.size(); i++) {
                VCarMoneyStatistic statistic = carMoneyStatistics.get(i);
                sliceValue = new SliceValue();
                sliceValue.setColor(Color.parseColor(ColorUtils.getRandColorCode()));
                sliceValue.setValue(statistic.getMoney());
                if (statistic.getType() == Constant.CAR_FINANCE_IN) {
                    switch (statistic.getMoneyType()) {
                        // 1运输公司充值 2.汽修厂充值 3.加油站充值 4运输公司取现 5.加油站取现
                        case TYPE_CZ_YSGS:
                            sliceValue.setLabel("运输公司充值总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_CZ_QXC:
                            sliceValue.setLabel("汽修厂充值总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_CZ_JYZ:
                            sliceValue.setLabel("加油站充值总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_CZ_YWSBFH:
                            sliceValue.setLabel("业务失败返回金额：￥" + statistic.getMoney() + "元");
                            break;
                    }
                } else if (statistic.getType() == Constant.CAR_FINANCE_OUt) {

                    switch (statistic.getMoneyType()) {
                        //1修车 2加油 3取现 4冲油卡  6转账 7业务取消
                        case TYPE_OUT_XC:
                            sliceValue.setLabel("修车使用总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_OUT_JY:
                            sliceValue.setLabel("加油使用总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_OUT_QX:
                            sliceValue.setLabel("总取现金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_OUT_CYC:
                            sliceValue.setLabel("冲油卡使用总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_OUT_ZZ:
                            sliceValue.setLabel("转账使用总金额：￥" + statistic.getMoney() + "元");
                            break;
                        case TYPE_OUT_YWQX:
                            sliceValue.setLabel("取销运费结算总金额：￥" + statistic.getMoney() + "元");
                            break;
                    }
                }
                sliceValueList.add(sliceValue);
            }

            PieChartData data = new PieChartData();
            data.setValues(sliceValueList);
            data.setHasCenterCircle(true);
            data.setCenterText1(centerText);
            data.setCenterText2(subText);
            data.setCenterText2Color(mContext.getResources().getColor(R.color.viewfinder_mask));
            data.setCenterCircleColor(mContext.getResources().getColor(R.color.write));

            pieChartView.setPieChartData(data);
            pieChartView.setOnValueTouchListener(getValueSelectListener());
            pieChartView.setCircleFillRatio(1);
            pieChartView.setChartRotation(45, false);
            pieChartView.setChartRotationEnabled(true);
        }

        public void setViewListener(final int position) {
            nestBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (lastPosition != -1) {
                        basePosition = lastPosition;
                    }
                    basePosition = (basePosition - 1 + sliceValueList.size()) % sliceValueList.size();
                    SelectedValue selectedValue =
                            new SelectedValue(basePosition, 0, SelectedValue.SelectedValueType.NONE);
                    pieChartView.selectValue(selectedValue);
                }
            });
        }
    }


    public PieChartOnValueSelectListener getValueSelectListener() {
        PieChartOnValueSelectListener listener = new PieChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int arcIndex, final SliceValue value) {
                Snackbar snackbar = Snackbar
                        .with(mContext)
                        .type(SnackbarType.MULTI_LINE)
                        .duration(Snackbar.SnackbarDuration.LENGTH_SHORT)
                        .position(Snackbar.SnackbarPosition.BOTTOM)
                        .margin(15, 15)
                        .backgroundDrawable(R.color.colorPrimary)
                        .text(String.valueOf(value.getLabel()))
                        .textColor(Color.WHITE)
                        .actionLabel("查看")
                        .actionColor(Color.WHITE)
                        .actionListener(new ActionClickListener() {
                            @Override
                            public void onActionClicked(Snackbar snackbar) {
                                ToastUtils.showShort(value.getLabel().toString());
                            }
                        });
                SnackbarManager.show(snackbar);
            }

            @Override
            public void onValueDeselected() {

            }
        };
        return listener;
    }
}
